//index.js
//获取应用实例
const utils = require('../../utils/util.js');
const app = getApp();
Page({
    data: {
        count: 0,
        pindex: 1,
        param:"",
        act: "getzx", //默认是最新
        bannel: [],
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 500,
        circular: true,

        //中间的选择器
        product_recommend: [
            {
            name: '最新发布',
            id: 1,
            selected: 'selected'
            },
            {
                name: '最热',
                id: 3,
                selected: ''
            }
        ],
        product_list: [],
    },
    bannelclick: function (e) {
        wx.navigateTo({
          url: '../vote/detail?id=' + e.currentTarget.dataset.id,
        })
    },
     //  / pages / product / details ? id =
    //页面上拉触底事件的处理函数
    onReachBottom: function () {
        var $this = this;
        $this.data.pindex = $this.data.pindex + 1;

        console.log('ppppp='+$this.data.pindex);
        //需要去请求数据
        utils.geturl({
            "handler": "Home",
            "method":"homeData",
            "param": '{"p":"' + $this.data.pindex + '","act":"'+$this.data.act+'"}',
        }, function (res) {
            if (res.code == 200) {

                if (res.data.obj.vote.length == 0) {
                    wx.showToast({
                        title: '已无更多数据',
                        icon: "none"
                    });
                }
                for (var i = 0; i < res.data.obj.vote.length; i++) {
                    $this.data.product_list.push(res.data.obj.vote[i]);
                }
                // $this.data.product_list.push(res.data.plist);
                $this.setData({
                    "product_list": $this.data.product_list
                });
            } else {
                wx.showToast({
                    title: '服务器错误,请稍候再试',
                    icon: "none"
                });
            }
        });

    },

    onLoad: function () {
        var $this = this;
        $this.getHomeData();
    },


    //获取数据
    getHomeData: function(){
        wx.showLoading({
            title: '玩命加载中',
        });
        var $this = this;

        let isAjax = true;
        //需要去请求数据

        if (isAjax){
            isAjax = false;
            utils.geturl({
                "handler": "Home",
                "method":"homeData",
                "param": '{"p":"' + $this.data.pindex + '","act":"'+$this.data.act+'"}',
            }, function (res) {
                console.log(res);
                console.log(typeof res);

                $this.setData({
                    "bannel": res.data.obj.banner,
                    "produty_type": res.data.obj.vote_type,
                    "product_list": res.data.obj.vote,
                    "count": res.data.obj.count
                });
                isAjax = true;

            });
        }
    },

    //推荐点击
    recclick: function (e) {
        var $this = this;
        for (var item in this.data.product_recommend) {

            if (this.data.product_recommend[item].id == e.target.dataset.id && this.data.product_recommend[item].selected == "") {
                this.data.product_recommend[item].selected = 'selected';

                if (e.target.dataset.id == 1) {
                    $this.data.param = '{"act":"new","p":"1"}';
                    $this.data.pindex = 1;
                    $this.data.act = 'new';
                }else if (e.target.dataset.id == 3) {
                    $this.data.param = '{"act":"mj","p":"1"}';
                    $this.data.pindex = 1;
                    $this.data.act = 'mj';
                }
            } else {
                this.data.product_recommend[item].selected = '';
            }
        }

        // console.log(this.data.product_recommend);
        // Page.prototype.setData();
        this.setData({
            "product_recommend": this.data.product_recommend
        });
        console.log($this.data.act);
        if ($this.data.act != "") {

            let isAjax = true;

            if (isAjax){

                isAjax = false;
                //需要去请求数据
                utils.geturl({
                    "handler": "Home",
                    "method":"homeData",
                    "param": $this.data.param
                }, function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        $this.setData({
                            "product_list": res.data.obj.vote
                        });
                    } else {
                        wx.showToast({
                            title: '服务器错误,请稍候再试',
                            icon: "none"
                        });
                    }
                    isAjax = true;
                });
            }
        }
    },
    //类型点击
    typeclick: function (e) {
        console.log(e);
        wx.navigateTo({
            url: '/pages/product/list?id=' + e.target.dataset.id,
        })
    },
    productclick: function () {
        console.log(Math.random());
    }

})